package cn.iocoder.yudao.module.hs.service.materiallog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.materiallog.vo.MaterialLogPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.materiallog.vo.MaterialLogSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materiallog.MaterialLogDO;
import cn.iocoder.yudao.module.hs.dal.mysql.materiallog.MaterialLogMapper;
import cn.iocoder.yudao.module.hs.service.material.MaterialService;
import cn.iocoder.yudao.module.hs.service.materiallog.bo.MaterialLogSaveReqBO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.MATERIAL_LOG_NOT_EXISTS;

/**
 * 物料日志 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class MaterialLogServiceImpl implements MaterialLogService {

    @Resource
    private MaterialLogMapper materialLogMapper;
    @Resource
    private MaterialService materialService;

    @Override
    public Long createMaterialLog(MaterialLogSaveReqVO createReqVO) {
        // 插入
        MaterialLogDO materialLog = BeanUtils.toBean(createReqVO, MaterialLogDO.class);
        materialLogMapper.insert(materialLog);
        // 返回
        return materialLog.getId();
    }

    @Override
    @Async
    public void batchCreateMaterialLog(Long userId, Integer operateType, List<Long> materialIds, String content) {
        materialIds.forEach(materialId -> {
            // 物料信息
            MaterialDO material = materialService.getMaterial(materialId);
            // 记录日志
            MaterialLogSaveReqBO saveBO = new MaterialLogSaveReqBO()
                    .setUserId(userId).setMaterialId(materialId).setMaterialName(material.getName())
                    .setMaterialCode(material.getCode()).setMaterialModeCode(material.getModeCode())
                    .setWorkOrderNo(material.getWorkOrderNo()).setWarehouseId(material.getWarehouseId())
                    .setMaterialBoxCode(material.getBoxCode()).setMaterialTrayCode(material.getTrayCode())
                    .setOperateType(operateType).setContent(content);
            createMaterialLog(saveBO);
        });
    }

    @Async
    @Override
    public void createMaterialLog(MaterialLogSaveReqBO createReqBO) {
        materialLogMapper.insert(BeanUtils.toBean(createReqBO, MaterialLogDO.class));
    }

    @Override
    public void updateMaterialLog(MaterialLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialLogExists(updateReqVO.getId());
        // 更新
        MaterialLogDO updateObj = BeanUtils.toBean(updateReqVO, MaterialLogDO.class);
        materialLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialLog(Long id) {
        // 校验存在
        validateMaterialLogExists(id);
        // 删除
        materialLogMapper.deleteById(id);
    }

    private void validateMaterialLogExists(Long id) {
        if (materialLogMapper.selectById(id) == null) {
            throw exception(MATERIAL_LOG_NOT_EXISTS);
        }
    }

    @Override
    public MaterialLogDO getMaterialLog(Long id) {
        return materialLogMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialLogDO> getMaterialLogPage(MaterialLogPageReqVO pageReqVO) {
        return materialLogMapper.selectPage(pageReqVO);
    }

}
